package net.csdn.business.notify.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.notify.common.model.entity.NotifyPrivateLetter;
import net.csdn.business.notify.common.model.query.NotifyPrivateLetterQuery;
import net.csdn.business.notify.common.model.vo.NotifyPrivateLetterVO;
import net.csdn.business.notify.common.model.vo.UserPrivateLetterVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 通知中心-用户私信Mapper接口
 * 
 * @author ruoyi
 * @date 2023-11-27
 */
public interface NotifyPrivateLetterMapper extends BaseMapper<NotifyPrivateLetter>
{

    /**
     * 分页查询通知中心-用户私信列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Select("<script> select * from notify_private_letter where 1=1 " +
            "<if test='query.senderId != null and query.senderId!=\"\"' >" +
            " AND sender_id=#{query.senderId} " +
            "</if>" +
            "<if test='query.content != null and query.content!=\"\"' >" +
            " AND content like concat('%', #{query.content}, '%')  " +
            "</if>" +
            "<if test='query.receiverId != null and query.receiverId!=\"\"' >" +
            " AND receiver_id=#{query.receiverId} " +
            "</if>" +
            "<if test='query.batchNum != null and query.batchNum!=\"\"' >" +
            " AND batch_num=#{query.batchNum} " +
            "</if>" +
            "<if test='query.readState != null ' >" +
            " AND read_state=#{query.readState} " +
            "</if>" +
            "<if test='query.since != null ' >" +
            " AND created_date>=#{query.since} " +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyPrivateLetterVO> findPageList(Page page, @Param("query") NotifyPrivateLetterQuery query);

    /**
     * 查询通知中心-用户私信
     * 
     * @param id 通知中心-用户私信主键
     * @return 通知中心-用户私信
     */
    @Select("<script> select * from notify_private_letter where object_id=#{id} </script>")
    public NotifyPrivateLetterVO selectNotifyPrivateLetterById(String id);

    /**
     * 查询通知中心-用户私信列表
     * 
     * @param query 通知中心-用户私信
     * @return 通知中心-用户私信集合
     */
    @Select("<script> select * from notify_private_letter where 1=1 " +
            "<if test='query.senderId != null and query.senderId!=\"\"' >" +
            " AND sender_id=#{query.senderId} " +
            "</if>" +
            "<if test='query.receiverId != null and query.receiverId!=\"\"' >" +
            " AND receiver_id=#{query.receiverId} " +
            "</if>" +
            "<if test='query.batchNum != null and query.batchNum!=\"\"' >" +
            " AND batch_num=#{query.batchNum} " +
            "</if>" +
            "<if test='query.readState != null ' >" +
            " AND read_state=#{query.readState} " +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyPrivateLetterVO> selectNotifyPrivateLetterList(@Param("query") NotifyPrivateLetterQuery query);


    /**
     * 删除通知中心-用户私信
     * 
     * @param id 通知中心-用户私信主键
     * @return 结果
     */
    @Delete("<script> delete from notify_private_letter where object_id= #{id} </script>")
    public Integer deleteNotifyPrivateLetterById(String id);


    @Insert("<script> insert into notify_private_letter (object_id,dialogue_id,batch_num,sender_id," +
            " receiver_id,content,is_system,created_date" +
            " ) VALUES " +
            " <foreach collection ='list' item='entity' index= 'index' separator =','> " +
            " ( " +
            "  #{entity.objectId},#{entity.dialogueId}, #{entity.batchNum},#{entity.senderId}," +
            " #{entity.receiverId},#{entity.content},#{entity.isSystem},#{entity.createdDate}" +
            " ) " +
            " </foreach> " +
            "</script>")
    public Integer insertBatch(@Param("list") List<NotifyPrivateLetter> list);




    /**
     * 前台用户私信列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Select("<script> select t1.*  from notify_private_letter t1 inner join " +
            " (select max(id) as id from notify_private_letter where " +
            " sender_id=#{query.currUserId} or receiver_id=#{query.currUserId} group by dialogue_id ) t2 " +
            " ON t1.id = t2.id order by t1.created_date desc " +
            "</script>")
    public List<UserPrivateLetterVO> userPrivatePage(Page page, @Param("query") NotifyPrivateLetterQuery query);



    @Select("<script> select * from notify_private_letter where dialogue_id=#{query.dialogueId} " +
            " order by created_date desc " +
            "</script>")
    public List<UserPrivateLetterVO> userDialoguePage(Page page, @Param("query") NotifyPrivateLetterQuery query);




    @Update("<script> update notify_private_letter set read_state=1,read_date=now() where " +
            " read_state=0 and object_id in " +
            " <foreach collection ='idList' item='id' open='(' separator =','  close=')'> " +
            " #{id}" +
            " </foreach> " +
            "</script>")
    public Integer updateReadState(@Param("idList") List<String> idList);


    @Select("<script> select count(*) from notify_private_letter where receiver_id=#{currUserId}  and read_state=0 " +
            "</script>")
    public int getUnReadCount(@Param("currUserId")String currUserId);


    @Update("<script> update notify_private_letter set read_state=1,last_modified_by=#{currUserId} where receiver_id=#{currUserId} and read_state=0" +
            "</script>")
    public int markRead(@Param("currUserId")String currUserId);
}
